
var netUtil = require('/../../utils/netUtil.js')
var util = require('/../../utils/util.js')
var app = getApp();

Page({
  
  data:{
 		accountPayType : null,
 		accountCode : null,
 		accountAccessQueryResult : null,   //查询出入仓明细的返回结果
 		accountStorageQueryResult : null   //查询仓租明细的返回结果
  },
  onLoad:function(options){
  	
  	var accountPayType = options.accountPayType;
  	var accountCode = options.accountCode;
  	
  	if(accountPayType != undefined && accountCode != undefined) {
  		this.setData({
  			accountPayType : accountPayType,
  			accountCode : accountCode
  		});
  	}
  	
  	//这里需要修改，现在 出仓入仓归为一类，后面要分开
  	if(accountPayType == '2' || accountPayType == '3') {    //如果是要查询 出入仓费用
  		app.getUserOpedId(this.getAccountAccessQueryResult);
  	} else if(accountPayType == '1') {    //如果是要查询仓租费用
  		app.getUserOpedId(this.getAccountStorageQueryResult);
  	}
  	
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
  pay : function(e) {
  	var that = this;
  	
  	netUtil.showAlertDialog("提示", '确认支付?', true, "确定", null, function() {
  		var params = {
  			openId : app.globalData.userInfo.openid,
  			data : {
  				accountCode : that.data.accountCode
  			}
  		};
  	
  	netUtil.buildRequest(that,'/wisen/wechat/wxPay',params,{
  			onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				onSuccess : function(resp){
					
					netUtil.hideLoadingDialog(that);
					
					//如果请求成功
					if(resp.ret == app.globalData.respSuccessCode) {
						
						var data = resp.data;
						
						wx.requestPayment({
							'appId': data.appid,
  					  'timeStamp': data.timeStamp,
  					  'nonceStr': data.nonceStr,
   						'package': data.package,
   						'signType': 'MD5',
   						'paySign': data.paySign,
   						
   						'success':function(res){
   							
   							//如果支付成功，做响应处理
   							if(res.errMsg.indexOf('ok') >= 0 ) {
   								wx.navigateTo({
         						 url: '/pages/mypay/mypay'
     							 });
   							}
   							
  						},
  					  'fail':function(res){
  					  	
  					  	if(res.errMsg.indexOf('cancel') >= 0) {    //如果用户取消支付
  					  		netUtil.showAlertDialog("提示", "你已经取消支付", false, "确定", null, null);
  					  	} else {       //如果支付失败
  					  		 netUtil.showAlertDialog("提示", "支付失败", false, "确定", null, null);
  					  	}
  					  	
   						}
						})
						
					} else {
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
					}
				},
				onError: function(msgCanShow, code, hiddenMsg) {
       	  console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
  		}).send();
  	});
  	
  },
  
  //查询出入仓明细的返回结果
  getAccountAccessQueryResult : function() {
  		var that = this;
  		var params = {
  			method : 'wechat.wisen.account.access.query',
  			openId : app.globalData.userInfo.openid,
  			data : {
  				accountCode : that.data.accountCode
  			}
  		};
  		
  		netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  			onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				onSuccess : function(resp){
					
					netUtil.hideLoadingDialog(that);
					
					//如果请求成功
					if(resp.ret == app.globalData.respSuccessCode) {
						
						var data = resp.data;
						
						for(var i=0;i<data.length;i++) {
							var obj = data[i];
							obj.payTime = util.dateFormatOnlyDate(obj.payTime);
						}
						
						that.setData({
							accountAccessQueryResult : data
						});
					} else {
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
					}
				},
				onError: function(msgCanShow, code, hiddenMsg) {
       	  console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
  		}).send();
  		
  },
  
  //查询仓租明细的返回结果
  getAccountStorageQueryResult : function() {
  		var that = this;
  		var params = {
  			method : 'wechat.wisen.account.storage.query',
  			openId : app.globalData.userInfo.openid,
  			data : {
  					accountCode : that.data.accountCode
  				}
  		};
  		
  			netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  				onPre: function() {
						netUtil.showLoadingDialog(that);
					},
					onSuccess : function(resp) {
						//如果请求成功
						if(resp.ret == app.globalData.respSuccessCode) {
						
							var data = resp.data;
							that.setData({
								accountStorageQueryResult : data
							});
						
						} else {
							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
						}
					},
					onError: function(msgCanShow, code, hiddenMsg) {
        		console.log("fail");
						netUtil.hideLoadingDialog(that);
						netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
					}
  		}).send();
  }


})